<?php 
// +----------------------------------------------------------------------
// | RXThinkCMF框架 [ RXThinkCMF ]
// +----------------------------------------------------------------------
// | 版权所有 2017~2020 南京RXThinkCMF研发中心
// +----------------------------------------------------------------------
// | 官方网站: http://www.rxthink.cn
// +----------------------------------------------------------------------
// | Author: 牧羊人 <1175401194@qq.com>
// +----------------------------------------------------------------------

namespace app\admin\controller;


use app\admin\service\OfPatentMonitorService;
use app\common\controller\Backend;
use think\facade\View;
use think\facade\Db;

/**
 * 专利监控管理-控制器
 * @author 牧羊人
 * @since: 2022/01/07
 * Class Ofpatentmonitor
 * @package app\admin\controller
 */
class Ofpatentmonitor extends Backend
{
    /**
     * 初始化方法
     * @author 牧羊人
     * @since: 2022/01/07
     */
    public function initialize()
    {
        parent::initialize(); // TODO: Change the autogenerated stub
        $this->model = new \app\admin\model\OfPatentMonitor();
        $this->service = new OfpatentmonitorService();
    }
	
    
    /**
     * 添加或编辑入口
     * @return mixed
     * @author 牧羊人
     * @date 2019/2/25
     */
    public function edit()
    {
        if (IS_POST) {
            $id = input("id",0);
            $apply_time  = input("apply_time",'');
            $issue_time  = input("issue_time",'');
            $pay_time    = input("pay_time",'');
            $notice_time = input("notice_time",'');
            $data['patent_name'] = input("patent_name",'');
            $data['number']      = input("number",'');
            $data['type']        = input("type",'');
            $data['company_name'] = input("company_name",'');
            $data['issue_time']  = !empty($issue_time)?strtotime($issue_time):0;
            $data['apply_time']  = !empty($apply_time)?strtotime($apply_time):0;
            $data['pay_time']    = !empty($pay_time)?strtotime($pay_time):0;
            $data['notice_time'] = !empty($notice_time)?strtotime($notice_time):0;
            $data['self_pay']    = input("self_pay",'');
            $data['status']      = input("status",'');
            $data['year']        = input("year",'');
            if(empty($id)){
                $result = Db::name("of_patent_monitor")->insert($data);
            }else{
                $result = Db::name("of_patent_monitor")->where("id",'=',$id)->update($data);
            }
            
            if($result !== false){
                $msg = message("更新成功", true);
            }else{
                $msg = message("更新失败", false);
            }
            return $msg;
        }
        $info = [];
        $id = input("get.id", 0);
        if ($id) {
            $info = $this->model->getInfo($id);
        } else {
            $data = func_get_args();
            if (isset($data[0])) {
                foreach ($data[0] as $key => $val) {
                    $info[$key] = $val;
                }
            }
        }
        View::assign("info", $info);
        return $this->render();
    }

    /**
     * 删除单条记录
     * @return array
     * @author 牧羊人
     * @date 2019/2/25
     */
    public function drop()
    {
        if (IS_POST) {
            $id = input('post.id');
            $info = $this->model->getInfo($id);
            if ($info) {
                $result = $this->model->drop($id);
                if ($result !== false) {
                    return message();
                }
            }
            return message($this->model->getError(), false);
        }
    }

    /**
     * 监控操作
     */
    public function exeUpdate()
    {
        $ids    = input("ids",0);
        $type   = input("type",0);
        $msg = message("执行失败", false);
        $rs = true;
        if($type == 1){
            $wh = [];
            $wh[] = ['id','in',$ids];
            $list = Db::name("of_patent_monitor")->where($wh)->select();
            if(!empty($list)){
                foreach ($list as $key => $value) {
                    $year = $value['year'];
                    $data = [];
                    $data['year']     = $year+1;
                    $data['pay_time'] = strtotime("+$year year",$value['apply_time']);
                    $data['type']     = 1;
                    Db::name("of_patent_monitor")->where('id','in',$value['id'])->update($data);
                }
            }
        }else if($type == 4){
            $rs = Db::name("of_patent_monitor")->where('id','in',$ids)->update(['self_pay'=>1]);
        }else if($type == 6){
            $rs = Db::name("of_patent_monitor")->where('id','in',$ids)->update(['status'=>0]);
        }
        if($rs){
            $msg = message("执行成功", true);
        }
        return json_encode($msg);
    }

    /**
     * 预览导出内容
     *      
     * 
     */
    public function mkdoc()
    {
        $ids = input("ids",'');
        $list = Db::name("of_patent_monitor")->where("id","in",$ids)->select()->toArray();
        $company_name = '';
        if(!empty($list)){
            foreach ($list as $key => $value) {
                if($key == 0){
                    $company_name = $value['company_name'];
                }
                $list[$key]['apply_time'] = $value['apply_time']>0?date("Y-m-d",$value['apply_time']):'';
                $list[$key]['issue_time'] = $value['issue_time']>0?date("Y-m-d",$value['issue_time']):'';
            }
        }
        View::assign("list", $list);
        View::assign("company_name", $company_name);
        View::assign("ids", $ids);

        $demo_list = Db::name("of_demo")->field("id,title,url")->where("mark","=",1)->select()->toArray();
        View::assign("demo_list", $demo_list);
        return $this->render();
    }


    /**
     * 预览导出内容
     *      
     * 
     */
    public function self()
    {
        $ids = input("ids",'');
        $list = Db::name("of_patent_monitor")->where("id","in",$ids)->select()->toArray();
        $company_name = '';
        if(!empty($list)){
            foreach ($list as $key => $value) {
                if($key == 0){
                    $company_name = $value['company_name'];
                }
                $list[$key]['apply_time'] = $value['apply_time']>0?date("Y-m-d",$value['apply_time']):'';
                $list[$key]['issue_time'] = $value['issue_time']>0?date("Y-m-d",$value['issue_time']):'';
            }
        }
        View::assign("list", $list);
        View::assign("company_name", $company_name);
        View::assign("ids", $ids);
        View::assign("pay_date", date("Y-m-d",strtotime("+1 month")));

        $demo_list = Db::name("of_demo")->field("id,title,url")->where("mark","=",1)->select()->toArray();
        View::assign("demo_list", $demo_list);
        return $this->render();
    }

    /**
     * 预览导出内容
     *      
     * 
     */
    public function company()
    {
        $ids = input("ids",'');
        $list = Db::name("of_patent_monitor")->where("id","in",$ids)->select()->toArray();
        $company_name = '';
        if(!empty($list)){
            foreach ($list as $key => $value) {
                if($key == 0){
                    $company_name = $value['company_name'];
                }
                $list[$key]['apply_time'] = $value['apply_time']>0?date("Y-m-d",$value['apply_time']):'';
                $list[$key]['issue_time'] = $value['issue_time']>0?date("Y-m-d",$value['issue_time']):'';
            }
        }
        View::assign("list", $list);
        View::assign("company_name", $company_name);
        View::assign("ids", $ids);

        $demo_list = Db::name("of_demo")->field("id,title,url")->where("mark","=",1)->select()->toArray();
        View::assign("demo_list", $demo_list);
        return $this->render();
    }

    /**
     * 预览导出内容
     *      
     * 
     */
    public function year()
    {
        $ids = input("ids",'');
        $list = Db::name("of_patent_monitor")->where("id","in",$ids)->select()->toArray();
        $company_name = '';
        if(!empty($list)){
            foreach ($list as $key => $value) {
                if($key == 0){
                    $company_name = $value['company_name'];
                }
                $list[$key]['apply_time'] = $value['apply_time']>0?date("Y-m-d",$value['apply_time']):'';
                $list[$key]['issue_time'] = $value['issue_time']>0?date("Y-m-d",$value['issue_time']):'';
            }
        }
        View::assign("list", $list);
        View::assign("company_name", $company_name);
        View::assign("ids", $ids);
        View::assign("pay_date", date("Y-m-d",strtotime("+1 month")));

        $demo_list = Db::name("of_demo")->field("id,title,url")->where("mark","=",1)->select()->toArray();
        View::assign("demo_list", $demo_list);
        return $this->render();
    }

    /**
     * 导出word文件
     */
    public function exportDoc()
    {
        $ids        = input("ids",'');
        $demo_id    = input("demo_id",0);
        $pay_date   = input("pay_date","");
        $yearly     = input("yearly/[]",'');
        $surrogate  = input("surrogate/[]",'');
        
        $list = Db::name("of_patent_monitor")->where("id","in",$ids)->select();

        $demo_info = Db::name("of_demo")->field("url,type")->where("id",$demo_id)->find();
        $demo_url  = $demo_info['url'];
        $demo_type = $demo_info['type'];

        $path = app()->getRootPath().'public/uploads';
        $templateFqfn = $path . $demo_url;
        $templateProcessor = new \PhpOffice\PhpWord\TemplateProcessor($templateFqfn);

        $company_name = '';
        $in_num = 0;    //N项发明
        $ex_num = 0;    //N项外观
        $pr_num = 0;    //N项实用
        $total  = 0;    //合计金额
        $year_total = 0;
        $ag_total = 0;
        if(!empty($list))
        {
            if(in_array($demo_type,[1,2])){
                $templateProcessor->cloneRow('l_company_name', count($list));
            }else{
                $templateProcessor->cloneRow('company_name', count($list));
            }
            $pay_time = time();
            foreach ($list as $key => $value) {
                if($key == 0){
                    $company_name = $value['company_name'];
                }
                if($demo_type == 1){
                    if($value['attr_type'] == 1){
                        $in_num ++;
                    }else if($value['attr_type'] == 2){
                        $ex_num ++;
                    }else if($value['attr_type'] == 3){
                        $pr_num ++;
                    }
                    // Simple table
                    $templateProcessor->setValue('l_company_name#'.($key+1), $value['company_name']);
                    $templateProcessor->setValue('l_patent_name#'.($key+1), $value['patent_name']);
                    $templateProcessor->setValue('l_number#'.($key+1), $value['number']);
                    $templateProcessor->setValue('l_apply_date#'.($key+1), date("Y-m-d",$value['apply_time']));
                    $templateProcessor->setValue('l_issue_date#'.($key+1), $value['issue_time']>0?date("Y-m-d",$value['issue_time']):'');
                }else if($demo_type == 2){
                    // Simple table
                    $templateProcessor->setValue('l_company_name#'.($key+1), $value['company_name']);
                    $templateProcessor->setValue('l_patent_name#'.($key+1), $value['patent_name']);
                    $templateProcessor->setValue('l_number#'.($key+1), $value['number']);
                    $templateProcessor->setValue('l_apply_date#'.($key+1), date("Y-m-d",$value['apply_time']));
                    $templateProcessor->setValue('l_money#'.($key+1), $yearly[$key]);
                    $total += $yearly[$key];
                }else if($demo_type == 3){
                    if($value['attr_type'] == 1){
                        $type = '实用新型';
                    }else if($value['attr_type'] == 2){
                        $type = '发明';
                    }else if($value['attr_type'] == 3){
                        $type = '外观设计';
                    }else if($value['attr_type'] == 4){
                        $type = '包授权发明专利';
                    }else if($value['attr_type'] == 5){
                        $type = '实用新型-发明同案申请';
                    }else if($value['attr_type'] == 6){
                        $type = '实用新型-发明同日申请';
                    }else if($value['attr_type'] == 6){
                        $type = '发明-实用新型同日申请';
                    }else{
                        $type = '发明-实用新型同案申请';
                    }
                    

                    $yearlys = (isset($yearly[$key]) && !empty($yearly[$key]))?$yearly[$key]:0;
                    $surrogates = (isset($surrogate[$key]) && !empty($surrogate[$key]))?$surrogate[$key]:0;
                    $templateProcessor->setValue('person#'.($key+1), $value['company_name']);
                    $templateProcessor->setValue('type#'.($key+1), $type);
                    $templateProcessor->setValue('name#'.($key+1), $value['patent_name']);
                    $templateProcessor->setValue('no#'.($key+1), $value['number']);
                    $templateProcessor->setValue('date#'.($key+1), date("Y-m-d",$value['apply_time']));
                    $templateProcessor->setValue('status#'.($key+1), $value['year']);
                    $templateProcessor->setValue('year_fees#'.($key+1), $yearlys);
                    $templateProcessor->setValue('ag_fees#'.($key+1), $surrogates);
                    $year_total += $yearlys;
                    $ag_total += $surrogates;
                }
                if($pay_time > $value['pay_time']){
                    $pay_time = $value['pay_time'];
                }
            }
            if(empty($pay_date)){
                $pay_date = date("Y-m-d",$pay_time);
            }
        }
        if($demo_type == 1){
            $templateProcessor->setValue('in_num',$in_num);
            $templateProcessor->setValue('ex_num',$ex_num);
            $templateProcessor->setValue('pr_num',$pr_num);
        }else if($demo_type == 2){           
            $templateProcessor->setValue('total',$total);
            $templateProcessor->setValue('pay_date',$pay_date);
            $templateProcessor->setValue('num',count($list));
        }else if($demo_type == 3){
            $templateProcessor->setValue('pay_date',$pay_date);
            $templateProcessor->setValue('year_total',$year_total);
            $templateProcessor->setValue('ag_total',$ag_total);
            $templateProcessor->setValue('num',count($list));
        }
        $templateProcessor->setValue('company_name', $company_name);

        $filename = "/demo/download/".$company_name.'-'.date("YmdHis").'.docx';
        $templateProcessor->saveAs($path.$filename);

        if(is_file($path.$filename)){
            $msg = message("执行成功",true,$filename);
        }else{
            $msg = message("执行失败", false);
        }
        return $msg;
    }
}